package com.ittest.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.ittest.domain.system.Permission;
import com.ittest.domain.system.Role;
import com.ittest.domain.system.User;
import com.ittest.factory.MapperFactory;
import com.ittest.mapper.system.PermissionMapper;
import com.ittest.mapper.system.RoleMapper;
import com.ittest.mapper.system.UserMapper;
import com.ittest.service.system.UserService;
import com.ittest.utils.MD5Util;
import com.ittest.utils.TransactionUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    @Override
    public void updatePwdById(String id, String newPwd) {
        //利用工具类获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //利用工具类获取mapper对象
        UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
        //调用mapper接口中的方法
        mapper.updatePwdById(id,newPwd);
        //提交事务
        TransactionUtil.commit(sqlSession);
        //释放资源
        TransactionUtil.close(sqlSession);
    }

    @Override
    public User login(String username, String password) {
        // 获取SQLSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        // 获取mapper对象
        UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
        // 调用mapper层方法
        return mapper.findByUsernameAndPwd(username,password);
    }

    @Override
    public List<Permission> findPermissionById(String id) {
        // 获取SQLSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
        // 获取mapper对象
        PermissionMapper mapper = MapperFactory.getMapper(sqlSession, PermissionMapper.class);
        // 调用mapper层方法
        return mapper.findPermissionByUserId(id);

    }

    @Override
    public List<User> findAll(int page, int size,String sname) {
        SqlSession sqlSession = null;
        List<User> list = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            //设置分页参数
            PageHelper.startPage(page,size);
            // sname非空判断
            if (StringUtils.isNotBlank(sname)) {
                sname = sname.replace(" ","");
            }
            // 调用方法
            list = mapper.findAll(sname);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return list;
    }
    @Override
    public void save(User user) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取对应的mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            // 随机生成id
            String id = UUID.randomUUID().toString();
            user.setId(id);

            // 密码加密
            user.setPassword(MD5Util.md5(user.getPassword()));
            // 调用持久层对应的方法
            mapper.save(user);
            // 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            // 记录日志
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }
    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            // 将id字符串切割
            String[] arr = id.split(",");
            // 调用持久层方法
            mapper.delete(arr);
            // 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 回滚事务
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            // 记录日志

        } finally {

            // 释放资源
            TransactionUtil.close(sqlSession);

        }
    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = null;
        User user = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            // 调用方法
            user = mapper.findById(id);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return user;
    }

    @Override
    public List<Role> findRoleByUserId(String id) {
        SqlSession sqlSession = null;
        List<Role> roles = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取mapper对象
            RoleMapper mapper = MapperFactory.getMapper(sqlSession, RoleMapper.class);
            // 调用方法
            roles = mapper.findRoleByUserId(id);
        } catch (Exception e) {
            throw  new RuntimeException(e);
        } finally {
            //释放资源
            TransactionUtil.close(sqlSession);
        }
        return roles;
    }

    @Override
    public void addRoleToUser(String userId, String[] roleIds) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取对应的mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            // 先删除所有的角色
            mapper.deleteRole(userId);
            for (String roleId : roleIds) {
                // 建立全新的角色关系
                mapper.updateRole(userId,roleId);
            }
            // 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            // 记录日志
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }
    @Override
    public void change(Integer status, String id) {
        SqlSession sqlSession = null;
        try {
            // 获取SQLSession对象
            sqlSession = MapperFactory.getSqlSession();
            // 获取对应的mapper对象
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            mapper.change(status,id);
            // 提交事务
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            // 事务回滚
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            // 记录日志
        } finally {
            // 释放资源
            TransactionUtil.close(sqlSession);
        }
    }
}
